package com.smart.sso.server.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.smart.mvc.service.mybatis.impl.ServiceImpl;
import com.smart.sso.server.dao.RoleLogDao;
import com.smart.sso.server.model.RoleLog;
import com.smart.sso.server.service.AppService;
import com.smart.sso.server.service.RoleLogService;
import com.smart.sso.server.service.RoleService;

@Service("roleLogService")
public class RoleLogServiceImpl extends ServiceImpl<RoleLogDao, RoleLog, Integer> implements RoleLogService {

	@Resource
	private RoleService roleService;
	@Resource
	private AppService appService;
	
	@Autowired
	public void setDao(RoleLogDao dao) {
		this.dao = dao;
	}
	
	public List<RoleLog> findByRoleId(Integer roleId) {
		return dao.findByRoleId(roleId);
	}

	public void allocate(Integer appId, Integer roleId, List<Integer> operationIdList) {
		dao.deleteByAppAndRoleId(appId, roleId);

		List<RoleLog> list = new ArrayList<RoleLog>();
		Integer operationId;
		for (Iterator<Integer> i$ = operationIdList.iterator(); i$.hasNext(); list
				.add(new RoleLog(appId, roleId, operationId))) {
			operationId = i$.next();
		}
		if (!CollectionUtils.isEmpty(list)) {
			super.save(list);
		}
		
		// JMS通知权限变更-Log的还未实现
		//logJmsService.send(appService.get(appId).getCode());
	}

	public void deleteByOperationIds(List<Integer> idList) {
		dao.deleteByOperationIds(idList);
	}

	public void deleteByRoleIds(List<Integer> idList) {
		dao.deleteByRoleIds(idList);
	}

	public void deleteByAppIds(List<Integer> idList) {
		dao.deleteByAppIds(idList);
	}

}
